//Display BMP image
//imshow('lena.bmp')

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<graphics.h>
#include<stdio.h>


#define widthoffset 18
#define pixeloffset 1078

unsigned char huge pixel[600][600];

void initialize(){
 int i,j;
 int gd=DETECT,gm,errorcode;
 struct palettetype pal;
 initgraph(&gd,&gm,"C:\\TurboC3\\BGI\\");
 errorcode = graphresult();
 if(errorcode != grOk)
 {
	printf("Something went wrong in BGI");

 }
 getpalette(&pal);

 for(i=0;i<pal.size;i++){
	setrgbpalette(pal.colors[i],i*4,i*4,i*4);
 }

}
void getdata(FILE *fp){

	int i,j,k;
	unsigned long wd,ht;

	fseek(fp,widthoffset,SEEK_SET);
	fread(&wd,4,1,fp);
	fread(&ht,4,1,fp);
	fseek(fp,pixeloffset,SEEK_SET);

	int height=(int)ht;
	int width=(int)wd;

	for(i=0;i<height;i++){
		for(j=0;j<width;j++){
			pixel[i][j]=fgetc(fp);

			putpixel(j,512-i,pixel[i][j]/16);
		}
	}
}

void main()
{

	FILE *fp;


	clrscr();
	initialize();


	char filename[25];
	printf("Enter file name:");
	scanf("%s",&filename);
	fp=fopen(filename,"rb");

	if(fp==NULL)
	{
	printf("file not exsist");
	}
	else
	{
		getdata(fp);
	}

	getch();
}
